package com.jackrain.nea.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.client.AsyncRestTemplate;

import java.net.URI;


/**
 * Created by nathan on 2017/8/23.
 */
@Service
@Slf4j
public class RestTemplateUtil {

//    @Autowired
//    RestTemplate restTemplate;

    @Autowired
    RestTemplateIn restTemplateIn;

    @Autowired
    AsyncRestTemplate asyncRestTemplate;

    public String doPostForm(URI uri, MultiValueMap<String, String> map) {

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
        headers.add("Accept", "*/*");
//        headers.add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);

        log.debug("request :" + request.toString());


        ResponseEntity<String> resp = restTemplateIn.getRestTemplate().postForEntity(uri, request, String.class);

        log.debug("resp :" + resp);

        return resp.getBody();

    }

    public String doPostForm(String url, MultiValueMap<String, String> map) {

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
        headers.add("Accept", "*/*");
//        headers.add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);

        log.debug("request :" + request.toString());

        ResponseEntity<String> resp = restTemplateIn.getRestTemplate().postForEntity(url, request, String.class);

        log.debug("resp :" + resp);

        return resp.getBody();

    }


    public String doGet(String url) {

        String ret = restTemplateIn.getRestTemplate().getForEntity(url, String.class).getBody();

        log.debug("ret :" + ret);


        return ret;
    }


    public void doPostAsync(String url, MultiValueMap<String, String> map, ListenableFutureCallback<ResponseEntity<String>> listenableFutureCallback) {

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
        headers.add("Accept", "*/*");
//        headers.add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);

        ListenableFuture<ResponseEntity<String>> forEntity = asyncRestTemplate.postForEntity(url, request, String.class);

        //异步调用后的回调函数
        forEntity.addCallback(listenableFutureCallback);
    }

    public void doGetAsync(String url, ListenableFutureCallback<ResponseEntity<String>> listenableFutureCallback) {


        ListenableFuture<ResponseEntity<String>> forEntity = asyncRestTemplate.getForEntity(url, String.class);

        //异步调用后的回调函数
        forEntity.addCallback(listenableFutureCallback);
    }

}
